package com.fz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fz.entity.Goods;
import com.fz.service.GoodsService;
import com.fz.vo.ResultVo;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 存储商户提交的商品信息 前端控制器
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    GoodsService goodsService;
    @GetMapping("/list")
    ResultVo<IPage<Goods>> list(Long categoryId, Integer pageNo, Integer pageSize) {
        return ResultVo.success(goodsService.getByCategoryId(categoryId, pageNo, pageSize));
    }
    @GetMapping("/getList")
    ResultVo<List<Goods>> getList() {
        return ResultVo.success(goodsService.list());
    }

    @GetMapping("/getMarketingGoods")
    ResultVo<List<Goods>> getMarketingGoods(Long marketingId) {
        return ResultVo.success(goodsService.getMarketingGoods(marketingId));
    }

    @GetMapping("/getHotList")
    ResultVo<IPage<Goods>> getlist( Integer pageNo, Integer pageSize) {
        return ResultVo.success(goodsService.getHotList( pageNo, pageSize));
    }

    @GetMapping("/getHotSalesGoods")
    ResultVo<List<Goods>> getHotSalesGoods() {
        return ResultVo.success(goodsService.getHotSalesGoods());
    }

    @GetMapping("/getByOrderAndCategory")
    ResultVo<IPage<Goods>> getByPriceOrderAndCategory(Long categoryId, Integer order, BigDecimal maxPrice, BigDecimal minPrice, Integer pageNo, Integer pageSize) {

        return ResultVo.success(goodsService.getByPriceOrderAndCategory(categoryId, order, maxPrice, minPrice, pageNo, pageSize));
    }

    @GetMapping("/getById")
    ResultVo<Goods> getById(Long id) {
        return ResultVo.success(goodsService.getById(id));
    }

    @GetMapping("/searchByGoodsName")
    ResultVo<IPage<Goods>> searchByGoodsName(String searchValue, Integer pageNo, Integer pageSize) {
        return ResultVo.success(goodsService.searchByGoodsName(searchValue, pageNo, pageSize));
    }
    @PostMapping("/add")
    ResultVo<Boolean> add(String name, String description, BigDecimal price, Integer store, Long categoryId, MultipartFile[] files) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVo.success(goodsService.add(name, description, price, store, categoryId,files), "添加商品成功");
    }

    @PostMapping("/del")
    ResultVo<Boolean> del(Long id) {
        return ResultVo.success(goodsService.removeById(id), "删除商品成功");
    }

    @PostMapping("/update")
    ResultVo<Boolean> update(Long id, String name, String description, BigDecimal price, Integer store, Long categoryId, MultipartFile[] files, String[] urls) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVo.success(goodsService.update(id,name,description,price,store,categoryId,files,urls),"修改商品成功");
    }

    @PostMapping("/updateStatus")
    ResultVo<Boolean> del(Long id, Boolean status) {
        // 根据设置的目标状态不同 返回不同的信息
        return ResultVo.success(goodsService.updateById(new Goods(id, status)), status ? "上架商品成功" : "下架商品成功");
    }

}
